
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="网络基础">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>网络基础 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        网络基础
      </h1>
      <span>
        
        <time class="time" datetime="2020-10-07T16:00:00.000Z">
        2020-10-08
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h1 id="网路基础"><a href="#网路基础" class="headerlink" title="网路基础"></a>网路基础</h1><h4 id="导读"><a href="#导读" class="headerlink" title="导读"></a>导读</h4><ul>
<li><p>早期的电话需要有电话线、后面的大屁股电脑需要插网线、再后来的智能手机电脑需要有网卡。</p>
<p>也就是说网络通信的第一步必须得先有<strong>物理连接介质</strong></p>
</li>
<li><p>有了物理连接介质就能实现通信了吗</p>
<p>不同语言国家的人彼此之间打电话沟通，彼此都无法理解对方说了些什么，需要双方都精通对方的语言或者用一种全世界<strong>通用的</strong>语言进行沟通交流才可以解决。</p>
<p>那回到我们的计算机，不同计算机之间要想实现无障碍的通信，除了物理连接介质以外，也应该有一套共用的标准，只有大家都按照这个标准来，计算机之间才能实现通信</p>
<p>这个标准我们叫它<strong>互联网协议</strong>！</p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214943803-705798806.gif" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214943803-705798806.gif" alt="111"></a></p>
</li>
</ul>
<h2 id="互联网协议"><a href="#互联网协议" class="headerlink" title="互联网协议"></a>互联网协议</h2><p>互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层  <strong>重点</strong>  </p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214944865-77757134..png" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214944865-77757134..png" alt="image-20190916192758331"></a></p>
<p><strong>每层常见运行协议</strong></p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214945829-524193847..png" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214945829-524193847..png" alt="image-20190916192901825"></a></p>
<h2 id="1-物理层"><a href="#1-物理层" class="headerlink" title="1.物理层"></a><strong>1.物理层</strong></h2><p>物理链接介质，传递电信号</p>
<h3 id="2-数据链路层"><a href="#2-数据链路层" class="headerlink" title="2.数据链路层"></a><strong>2.数据链路层</strong></h3><p>物理层接收到一长串010101如何切分取值，这个事数据链路层的里面的以太网协议</p>
<ul>
<li>1.它规定了0101的二进制电信号的分组方式</li>
<li>2.但凡接入互联网的机器，必须有一块网卡<ul>
<li>网卡(用来唯一标识全世界独一无二的一台计算机)每块网卡出厂时都会被烧制上一个全世界唯一的编号，通常由12位16进制数表示（前六位是厂商编号，后六位是流水线号)</li>
<li>这个唯一表示计算机独一无二的地址就叫mac地址</li>
</ul>
</li>
</ul>
<h2 id="3-网络层"><a href="#3-网络层" class="headerlink" title="3.网络层"></a><strong>3.网络层</strong></h2><p>规定了计算机都必须有一个ip地址</p>
<p>ip地址特点:点分十进制</p>
<p>有两个版本ipv4和ipv6 为了能够兼容更多的计算机</p>
<p>其中ipv4版本</p>
<p>​    最小:0.0.0.0</p>
<p>​    最大:255.255.255.255</p>
<p>IP协议可以跨局域网传输并且ip地址能够唯一标识互联网中独一无二的一台机器！</p>
<p>网络层：</p>
<p>私有地址：192.168.   172.      10.</p>
<p>回环地址： 127.0.0.1</p>
<h2 id="4-传输层"><a href="#4-传输层" class="headerlink" title="4.传输层"></a><strong>4.传输层</strong></h2><p>TCP/UDP协议都是基于端口工作的协议这是重点，后面再讲，先来看，计算机之间通信，到底是谁和谁想通信，是计算机本身吗？是不是应该是我安装在计算机上的应用软件之间想通信(qq,微信等)，那我们现在只是知道了如何定位全世界独一无二的计算机，没办法确定计算机之上的哪款软件需要通信吧?这就需要用到端口</p>
<p><strong>端口(port):</strong>0～65535用来标识一台计算机之上<strong>基于网络通信</strong>的应用软件，0～1024操作系统自己用的，一般情况下8000之后的端口我们可以任意使用，8000之前的通常会有相应的程序默认使用</p>
<p>​    ip：能够查到全世界独一无二的计算机</p>
<p>​    port:能够标识一台机器上的某个程序</p>
<p>​    ip+port:能够确定全世界范围内一台计算机上的某个程序</p>
<h3 id="TCP协议"><a href="#TCP协议" class="headerlink" title="TCP协议"></a><strong>TCP协议</strong></h3><p>当应用程序想通过TCP协议实现远程通信时，彼此之间必须先先建立双向通信通道，基于该双向通道实现数据的远程交互，该双向通道直到任意一方主动断开才会失效</p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214947782-1358252741..png" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214947782-1358252741..png" alt="image-20190916193622208"></a></p>
<h4 id="三次握手"><a href="#三次握手" class="headerlink" title="三次握手"></a>三次握手</h4><p>建立双向通道的过程称之为三次握手,建立通道的发起者可以是客户端也可以是服务端，下面我们就以客户端先主动发起为例</p>
<ul>
<li><p>客户端会朝服务端发送一个请求询问服务端:”我能不能挖一条通往你家的地道”</p>
</li>
<li><p>服务端收到请求，回复说:”好吧 你挖吧”，由于TCP是双向通道，客户端挖向服务端的通道只能给客户端朝服务端发消息使用，服务端要向给客户端发消息是没办法走这一条通道的，需要自己挖一条通往客户端的通道</p>
<p>所以服务端在回复同意客户端挖通道的同时还会问一句:”那我能不能也挖一条通往你家的通道”</p>
</li>
<li><p>客户端收到服务端请求后客户端到服务端的通道就挖成功了，然后也会同意服务端的请求，服务端挖向客户端的通道也会成功</p>
</li>
</ul>
<p>总结:之所以称之为三次握手就是因为中间的服务端的同意，和请求合并成了一次请求</p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214948388-305208614.gif" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214948388-305208614.gif" alt="222"></a></p>
<h4 id="四次挥手"><a href="#四次挥手" class="headerlink" title="四次挥手"></a>四次挥手</h4><p>建立一个连接需要三次握手，而终止一个连接要经过四次握手</p>
<p>当服务端或者客户端不想再与对方进行通信之后，双方任意一方都可以主动发起断开链接的请求，我们还是以客户端主动发起为例</p>
<ul>
<li><p>客户端由于已经没有任何需要发送给服务端的消息了，所以发起断开客户端到服务端的通道请求</p>
</li>
<li><p>服务端收到该请求后同意了 至此客户端到服务端的单项通道断开</p>
</li>
<li><p>服务端这个时候不会立刻朝客户端发器请求说那我也断开到你家的通道吧，服务端需要想想我手上还有没有需要发送给客户端的消息，如果还有的话，那我不能立马断开，先把数据发完才能断</p>
<p>等服务端检查完毕之后发送也没有数据要给客户端了，这个时候就会朝客户端发起断开服务端到客户端的通道请求</p>
</li>
<li><p>客户端同意该请求，至此四次挥手完成</p>
</li>
</ul>
<p>总结:挥手必须是四次，中间的两次不能合并成一次，原因就在于需要检查是否还有数据需要给对方发送</p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214948696-690441959.gif" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214948696-690441959.gif" alt="333"></a></p>
<h3 id="UDP协议"><a href="#UDP协议" class="headerlink" title="UDP协议"></a>UDP协议</h3><p>当应用程序希望通过UDP与一个应用程序通信时，传输数据之前源端和终端不建立连接。</p>
<p>当它想传送时就简单地去抓取来自应用程序的数据，并尽可能快地把它扔到网络上。</p>
<p>TCP和UDP类似于</p>
<p>​    TCP：打电话(你一句我一句，你侬我侬)</p>
<p>​    UDP: 发短信(管你看不看，发了就不关我事了)</p>
<p><a href="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214949134-1934760925.gif" target="_blank" rel="noopener"><img src="https://img2018.cnblogs.com/blog/1825659/201910/1825659-20191021214949134-1934760925.gif" alt="444"></a></p>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><p>TCP—传输控制协议,提供的是面向连接、可靠的字节流服务。当客户和服务器彼此交换数据前，必须先在双方之间建立一个TCP连接，之后才能传输数据。TCP提供超时重发，丢弃重复数据，检验数据，流量控制等功能，保证数据能从一端传到另一端。</p>
<p>UDP—用户数据报协议，是一个简单的面向数据报的运输层协议。UDP不提供可靠性，它只是把应用程序传给IP层的数据报发送出去，但是并不能保证它们能到达目的地。由于UDP在传输数据报前不用在客户和服务器之间建立一个连接，且没有超时重发等机制，故而传输速度很快</p>
<h2 id="5-应用层"><a href="#5-应用层" class="headerlink" title="5.应用层"></a>5.应用层</h2><p>应用层协议:<strong>http协议</strong>,<strong>ftp协议</strong>(目前了解即可)</p>
<p><a href="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569413618883&di=7744142ee93dd949c18e0f222f3a97fa&imgtype=0&src=http%3A%2F%2Fimg.wxcha.com%2Ffile%2F201811%2F01%2Faeae7124fe.jpg" target="_blank" rel="noopener"><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569413618883&di=7744142ee93dd949c18e0f222f3a97fa&imgtype=0&src=http%3A%2F%2Fimg.wxcha.com%2Ffile%2F201811%2F01%2Faeae7124fe.jpg" alt="img"></a></p>
<h2 id="基于TCP协议的socket"><a href="#基于TCP协议的socket" class="headerlink" title="基于TCP协议的socket"></a>基于TCP协议的socket</h2><h3 id="server端"><a href="#server端" class="headerlink" title="server端"></a>server端</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">sk = socket.socket()</span><br><span class="line">sk.bind((<span class="string">'127.0.0.1'</span>,<span class="number">8898</span>))  <span class="comment">#把地址绑定到套接字</span></span><br><span class="line">sk.listen()          <span class="comment">#监听链接</span></span><br><span class="line">conn,addr = sk.accept() <span class="comment">#接受客户端链接</span></span><br><span class="line">ret = conn.recv(<span class="number">1024</span>)  <span class="comment">#接收客户端信息</span></span><br><span class="line">print(ret)       <span class="comment">#打印客户端信息</span></span><br><span class="line">conn.send(<span class="string">b'hi'</span>)        <span class="comment">#向客户端发送信息</span></span><br><span class="line">conn.close()       <span class="comment">#关闭客户端套接字</span></span><br><span class="line">sk.close()        <span class="comment">#关闭服务器套接字(可选)</span></span><br></pre></td></tr></table></figure>

<h3 id="client端"><a href="#client端" class="headerlink" title="client端"></a>client端</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">sk = socket.socket()           <span class="comment"># 创建客户套接字</span></span><br><span class="line">sk.connect((<span class="string">'127.0.0.1'</span>,<span class="number">8898</span>))    <span class="comment"># 尝试连接服务器</span></span><br><span class="line">sk.send(<span class="string">b'hello!'</span>)</span><br><span class="line">ret = sk.recv(<span class="number">1024</span>)         <span class="comment"># 对话(发送/接收)</span></span><br><span class="line">print(ret)</span><br><span class="line">sk.close()            <span class="comment"># 关闭客户套接字</span></span><br></pre></td></tr></table></figure>

<h5 id="问题：有的同学在重启服务端时可能会遇到报错"><a href="#问题：有的同学在重启服务端时可能会遇到报错" class="headerlink" title="问题：有的同学在重启服务端时可能会遇到报错"></a>问题：有的同学在重启服务端时可能会遇到报错</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#加入一条socket配置，重用ip和端口</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> SOL_SOCKET,SO_REUSEADDR</span><br><span class="line">sk = socket.socket()</span><br><span class="line">sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,<span class="number">1</span>) <span class="comment">#就是它，在bind前加</span></span><br><span class="line">sk.bind((<span class="string">'127.0.0.1'</span>,<span class="number">8898</span>))  <span class="comment">#把地址绑定到套接字</span></span><br><span class="line">sk.listen()          <span class="comment">#监听链接</span></span><br><span class="line">conn,addr = sk.accept() <span class="comment">#接受客户端链接</span></span><br><span class="line">ret = conn.recv(<span class="number">1024</span>)   <span class="comment">#接收客户端信息</span></span><br><span class="line">print(ret)              <span class="comment">#打印客户端信息</span></span><br><span class="line">conn.send(<span class="string">b'hi'</span>)        <span class="comment">#向客户端发送信息</span></span><br><span class="line">conn.close()       <span class="comment">#关闭客户端套接字</span></span><br><span class="line">sk.close()        <span class="comment">#关闭服务器套接字(可选)</span></span><br></pre></td></tr></table></figure>

<h4 id="tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端"><a href="#tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端" class="headerlink" title="tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端"></a><strong>tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端</strong></h4><p>推荐的书：TCP/IP的三卷书</p>

    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#网路基础"><span class="toc-text">网路基础</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#导读"><span class="toc-text">导读</span></a></li></ol></li></ol><li class="toc-item toc-level-2"><a class="toc-link" href="#互联网协议"><span class="toc-text">互联网协议</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-物理层"><span class="toc-text">1.物理层</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-数据链路层"><span class="toc-text">2.数据链路层</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-网络层"><span class="toc-text">3.网络层</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-传输层"><span class="toc-text">4.传输层</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#TCP协议"><span class="toc-text">TCP协议</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#三次握手"><span class="toc-text">三次握手</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#四次挥手"><span class="toc-text">四次挥手</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#UDP协议"><span class="toc-text">UDP协议</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#总结"><span class="toc-text">总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-应用层"><span class="toc-text">5.应用层</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#基于TCP协议的socket"><span class="toc-text">基于TCP协议的socket</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#server端"><span class="toc-text">server端</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#client端"><span class="toc-text">client端</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#问题：有的同学在重启服务端时可能会遇到报错"><span class="toc-text">问题：有的同学在重启服务端时可能会遇到报错</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端"><span class="toc-text">tcp是基于链接的，必须先启动服务端，然后再启动客户端去链接服务端</span></a></li></ol></li>
  </div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/09/29/Go语言之指针/" rel="next" title="Go语言之指针">
          Go语言之指针
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/10/09/GitHub的高级使用/" rel="prev" title="GitHub的高级使用">
            GitHub的高级使用
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
